//
//  BIDReceivedNoticeViewController.m
//  cxgov
//
//  Created by mal on 16/3/6.
//  Copyright © 2016年 mal. All rights reserved.
//

#import "BIDReceivedOutingReportViewController.h"
#import "BIDReceivedOutingReportCell.h"
#import "BIDOutingReportDetailViewController.h"

//已收报备列表
static NSString *receivedNoticeListURL = @"outingAndroid!listReceiveOuting.action";
//删除报备
static NSString *deleteNoticeURL = @"outingAndroid!dropOutingReply.action";

@interface BIDReceivedOutingReportViewController ()<BIDBaseListViewDelegate>
{
    BOOL _bRegister;
}
@end

@implementation BIDReceivedOutingReportViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"已收报备";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.dataSourceArr removeAllObjects];
    self.curPage = 0;
    [self loadData];
}

- (void)loadData
{
    self.curPage++;
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], receivedNoticeListURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"outingReply.receiveUser=%@&page=%ld&rows=%ld", [AppDelegate getUserId], self.curPage, self.countPerPage];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(id responseObj, NSString *errMsg){
        if(responseObj)
        {
            NSDictionary *responseDic = (NSDictionary*)responseObj;
            [self.dataSourceArr addObjectsFromArray:responseDic[@"rows"]];
            if(self.dataSourceArr.count<self.curPage*self.countPerPage)
            {
                [self.myTableView hasNoMoreData];
            }
            [self.myTableView reloadData];
        }
    }];
}
//删除列表中的报备
- (void)deleteNoticeAtIndexPath:(NSIndexPath*)indexPath
{
    NSDictionary *dic = self.dataSourceArr[indexPath.row];
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], deleteNoticeURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"selectIds=%@", dic[@"outingId"]];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(id responseObj, NSString *errMsg){
        if(responseObj)
        {
            NSDictionary *responseDic = (NSDictionary*)responseObj;
            NSNumber *value = responseDic[@"isSuccess"];
            if([value intValue]==1)
            {
                [self.myTableView beginUpdates];
                [self.dataSourceArr removeObjectAtIndex:indexPath.row];
                [self.myTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
                [self.myTableView endUpdates];
            }
            else
            {
                [BIDGlobalMethods showAlertView:@"删除失败" buttonTitle:ALERT_BTN_TITLE delegate:nil tag:0];
            }
        }
    }];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    NSUInteger row = indexPath.row;
    NSUInteger section = indexPath.section;
    static NSString *identifier = @"identifier";
    if(!_bRegister)
    {
        _bRegister = YES;
        UINib *nib = [UINib nibWithNibName:@"BIDReceivedOutingReportCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:identifier];
    }
    BIDReceivedOutingReportCell *outingReportCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    NSDictionary *dic = self.dataSourceArr[row][@"outing"];
    
    outingReportCell.titleLabel.text = dic[@"reason"];
    NSString *strStartDate = dic[@"startTime"];
    NSString *strEndDate = dic[@"endTime"];
    outingReportCell.dateLabel.text = [[NSString alloc] initWithFormat:@"[%@-%@]", strStartDate, strEndDate];
    outingReportCell.nameLabel.text = [[NSString alloc] initWithFormat:@"申请人:%@", dic[@"createUserName"]];
    outingReportCell.placeLabel.text = [[NSString alloc] initWithFormat:@"地址:%@", dic[@"address"]];
    //已读、未读标记
    NSNumber *value = self.dataSourceArr[row][@"isRead"];
    if([value intValue] == 1)
    {
        [outingReportCell setReadFlag:YES];
    }
    else
    {
        [outingReportCell setReadFlag:NO];
    }
    cell = outingReportCell;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = self.dataSourceArr[indexPath.row];
    BIDOutingReportDetailViewController *vc = [[BIDOutingReportDetailViewController alloc] initWithNibName:@"BIDOutingReportDetailViewController" bundle:nil];
    vc.outingReportId = dic[@"id"];
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 103.f;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleDelete)
    {
        [self deleteNoticeAtIndexPath:indexPath];
    }
}

@end
